package com.neuronbit.businessflow.flow.source;

import com.neuronbit.businessflow.flow.source.util.FieldBuilder;
import com.neuronbit.businessflow.runtime.FlowContext;
import com.neuronbit.javaparser.ast.CompilationUnit;
import com.neuronbit.javaparser.ast.Modifier;
import com.neuronbit.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.neuronbit.javaparser.ast.body.FieldDeclaration;
import com.neuronbit.javaparser.ast.expr.NameExpr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The type Context java source.
 */
public class ContextJavaSource implements JavaSource {
    private final CompilationUnit cu;
    private final ClassOrInterfaceDeclaration contextClazz;

    /**
     * Instantiates a new Context java source.
     *
     * @param namePrefix  the name prefix
     * @param packageName the package name
     */
    public ContextJavaSource(String namePrefix, String packageName) {
        cu = new CompilationUnit(packageName);

        contextClazz = cu.addClass(namePrefix + "FlowContext");
        contextClazz.setFinal(true);
        contextClazz.setModifiers(Modifier.Keyword.PUBLIC);
        contextClazz.addExtendedType(FlowContext.class);

        cu.addImport(FlowContext.class);
    }

    /**
     * Gets fully qualified name.
     *
     * @return the fully qualified name
     */
    public String getFullyQualifiedName() {
        return contextClazz.getFullyQualifiedName().get();
    }

    /**
     * Gets package name.
     *
     * @return the package name
     */
    public String getPackageName() {
        return cu.getPackageDeclaration().get().getName().toString();
    }

    /**
     * Gets simple name.
     *
     * @return the simple name
     */
    public String getSimpleName() {
        return contextClazz.getNameAsString();
    }

    /**
     * Add field field declaration.
     *
     * @param fieldBuilder the field builder
     * @return the field declaration
     */
    public FieldDeclaration addField(FieldBuilder fieldBuilder) {
        return fieldBuilder.buildInto(contextClazz);
    }

    @Override
    public void generate() {

    }

    @Override
    public CompilationUnit getCompilationUnit() {
        return cu;
    }

    @Override
    public String getFileName() {
        return getSimpleName();
    }

    /**
     * Add import.
     *
     * @param type the type
     */
    public void addImport(String type) {
        cu.addImport(type);
    }
}
